package com.free.freemarker.module;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.free.freemarker.constant.Constant;
import com.free.freemarker.core.SqlBuilder;
import com.free.freemarker.model.Annotation;
import com.free.freemarker.utils.DateUtils;
import com.free.mysql.model.Table;

/**
 * 数据建立
 *
 * @author 66
 */
public class Model {

    private Map<String, Object> root = new HashMap<String, Object>();

    private SqlBuilder sqlBuilder;

    public static Model builder(SqlBuilder sqlBuilder) {
        Model model = new Model();
        model.sqlBuilder = sqlBuilder;
        model.rootBuilder();
        return model;
    }

    public void put(String key, Object value) {
        root.put(key, value);
    }

    public void remove(String key) {
        root.remove(key);
    }


    public Object get(String key) {
        return root.get(key);
    }


    public void clear() {
        root.clear();
    }

    public Map<String, Object> getRoot() {
        return root;
    }

    public void rootBuilder() {
        Annotation annotation = Annotation.build(Constant.ANNOTATION_AUTHOR_NAME, Constant.ANNOTATION_AUTHOR_MAIL, DateUtils.getTime(), Constant.ANNOTATION_VERSION);
        root.put(Constant.ANNOTATION, annotation);
    }

    public Table rootTableBuilder(String tableName) throws Exception {
        Connection con = sqlBuilder.createConnection();
        Table t = sqlBuilder.getMetaData(tableName, con);
        con.close();
        return t;
    }
}
